﻿using System.ServiceModel;
using Ninject;

namespace gixoom.Initiator.WcfWsHttp
{
    public class gixoomWcfWsHttpInitiatorFacade: IgixoomWcfWsHttpInitiatorFacade
    {
        private IKernel kernel;

        /// <summary>
        /// Creates gixoomDomFacade object.
        /// </summary>
        /// <param name="kernel">the kernel</param>
        /// <exception cref="System.ArgumentNullException"><i>kernel</i> is a null reference.</exception>
        public gixoomWcfWsHttpInitiatorFacade(IKernel kernel)
        {
            Ensure.ArgumentNotNull(kernel, "kernel");

            this.kernel = kernel;
        }

        /// <summary>
        /// Creates initiator block storage.
        /// </summary>
        /// <param name="address">WCF host address</param>
        /// <returns>created initiator block storage</returns>
        /// <exception cref="System.ArgumentNullException"><i>address</i> is a null reference.</exception>
        /// <exception cref="System.ArgumentException"><i>address</i> is an empty string.</exception>
        public IInitiatorBlockStorage CreateInitiatorBlockStorage(string address)
        {
            Ensure.ArgumentNotNull(address, "address");
            Ensure.StringArgumentNotEmpty(address, "address");

            var binding = new WSHttpBinding();
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
            binding.Security.Mode = SecurityMode.None;
            var endpoint = new EndpointAddress(address);
            var client = new gixoomWcfWsHttpTarget.TargetBlockStorageClient(binding, endpoint);
            var blockStorage = new InitiatorBlockStorage(client);

            return blockStorage;
        }
    }
}
